import java.util.*;

public class Test {
        public int numJewelsInStones(String jewels, String stones) {
            Set<Character> set= new HashSet<>();
            int count = 0;
            for(int i=0;i<jewels.length();i++){
                char ch=jewels.charAt(i);
                set.add(ch);
            }
            for(int i=0;i<stones.length();i++){
                char ch=stones.charAt(i);
                if(set.contains(ch)){
                    count++;
                }
            }
            return count;
        }
        //toCharArray() 方法将字符串转换为字符数组
        public static void func(String str1,String str2){
            Set<Character> set = new HashSet<>();
            for(char ch:str2.toUpperCase().toCharArray()){
                set.add(ch);
            }
            Set<Character> setBroken = new HashSet<>();
            for(char ch:str1.toUpperCase().toCharArray()){
                if(!set.contains(ch)&&!setBroken.contains(ch)){
                    setBroken.add(ch);
                    System.out.print(ch);
                }
            }
        }
    public static void main1(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()){
            String str1=scan.nextLine();
            String str2=scan.nextLine();
            func(str1,str2);
        }
    }
    public static List<String> topKFrequent(String[] words, int k) {
            //1\遍历数组，统计每个单词出现的频率
            Map<String,Integer> hashMap=new HashMap<>();
        for (String s:words) {
            if(hashMap.get(s)==null){
                hashMap.put(s,1);
            }else{
                hashMap.put(s,hashMap.get(s)+1);
            }
        }//2、建立小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap=new PriorityQueue<>(
                k, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue())==0){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        }
        );
        //3、遍历hashMap 把里面的数据放到小根堆
        for(Map.Entry<String,Integer> entry:hashMap.entrySet()){
            if(minHeap.size()<k){
                minHeap.offer(entry);
            }else{
                //小根堆放满了k个 下一个entry和堆顶元素比较
                Map.Entry<String,Integer> top = minHeap.peek();
                //堆顶的频率小于当前entry的频率 就出队 入队entry
                if(top.getValue().compareTo(entry.getValue())<0){
                    minHeap.poll();
                    minHeap.add(entry);
                }else {//频率相同的情况
                    if(top.getValue().compareTo(entry.getValue())==0){
                        if(top.getKey().compareTo(entry.getKey())>0){
                            minHeap.poll();
                            minHeap.add(entry);
                        }
                    }
                }
            }
        }
        //4、此时小根堆当中已经有了结果
        //System.out.println(minHeap);
        List<String> ret = new ArrayList<>();
        for (int i=0;i<k;i++) {
            String key=minHeap.poll().getKey();
            ret.add(key);
        }
        Collections.reverse(ret);//小根堆弹出是从小到大 需要反转变从大到小
        //System.out.println("ret:"+ret);
        return ret;
    }

    public static void main(String[] args) {
        //String[] words={"i", "love", "leetcode", "i", "love", "coding"};
        String[] words={"i","love","leetcode","i","love","coding"};
        List<String> ret = topKFrequent(words,3);
        System.out.println(ret);
    }
}
